﻿
namespace TestFsRx

open System
open System.Reactive
open System.Reactive.Linq
open System.Reactive.Subjects
open FSharp.Reactive
open NUnit.Framework
open FsUnit
open TestHelpers

[<TestFixture>]
type ErrorFinally()=

    [<Test>]
    member this.InvokeWhenCompleted() =
        let invoked = ref false

        Observable.Return 100
        |> Observable.performFinally (fun() -> invoked := true)
        |> shouldObserve 1 false true

        !invoked |> should be True

    [<Test>]
    member this.InvokeWhenError() =
        let invoked = ref false

        Observable.Return 99
        |> Observable.concat (makeErrorStream<int,InvalidOperationException>())
        |> Observable.performFinally(fun() -> invoked := true)
        |> shouldObserve 1 true false

        !invoked |> should be True

    [<Test>]
    member this.InvokeWhenDisposed() =
        let invoked = ref false
        let error = ref false
        let completed = ref false

        let subject = new Subject<int>()
        let subscription =
            subject 
            |> Observable.performFinally (fun() -> invoked := true)
            |> Observable.subscribe ignore (fun _->error := true) (fun() -> completed := true)

        for index = 1 to 3 do
            subject.OnNext index
        subscription.Dispose()
        
        !invoked |> should be True
        !error |> should be False
        !completed |> should be False





        

